package security;

/**
 *
 * @author MONESI
 */
import boundary.EventManager;
import boundary.InviteManager;
import boundary.SearchEventManager;
import boundary.SearchManager;
import boundary.UserManager;
import entity.ResearchEvent;
import entity.ResearchUser;
import entity.User;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.persistence.Query;

@Named
@SessionScoped
public class SearchEventBean implements Serializable {
    
    @EJB
    private SearchEventManager sm;
    
    @EJB
    private EventManager em;
    
    @EJB
    private InviteManager im;
    
    @EJB
    private UserManager um;
    
    ResearchEvent event;
    
    
    public SearchEventBean() {
    }
    
     public ResearchEvent getEvent() {
        if (event == null) {
            event = new ResearchEvent();
        }
        return event;
    }
    
    
    public void setEvent (ResearchEvent event) {
        this.event = event;
    }
    
         
    public String research() {
        int esiste=0;
        int esiste2=0;
        
        for(int i=0;i<em.getEventList(um.getLoggedUser().getEmail()).size() && esiste==0;i++){     
            if(event.getIdEvento() == em.ListId(um.getLoggedUser().getEmail()).get(i)){
                esiste=1;    
            }
        }
        if(esiste==1){
            
            sm.save(event);
        
                return "searchedEvent";
            
        }else if(esiste==0){
            
            for(int j=0;j<im.listAcceptedEvents(um.getLoggedUser().getEmail()).size() && esiste2==0;j++){
                
                if(event.getIdEvento() == im.listAcceptedEventsId(um.getLoggedUser().getEmail()).get(j)){
                    esiste2=1;    
                }
            }
            if(esiste2==1){
                sm.save(event);
                return "searchedEventInvited";
            }else
                return "notExistsEvent";
        }
            
        return "";
        
    }
    
    public String name (){
        return sm.name();
    }
    public String place (){
        return sm.place();
    }
    public Date startDate (){
        return sm.startDate();
    }
    public Date endDate (){
        return sm.endDate();
    }
    
    
    public String description (){
        return sm.description();
    }
    public String creatore (){
        return sm.creatore();
    }
    public String delete (int k){
        sm.delete(k);
        return "ScheduleView";
    }
    
    public int id (){
        return event.getIdEvento();
    }

}